# 用户行为监控调试测试脚本
$baseUrl = "http://localhost:5000"
$sessionId = "debug_session_$(Get-Date -Format 'yyyyMMddHHmmss')"

Write-Host "开始用户行为监控调试测试..." -ForegroundColor Green
Write-Host "SessionId: $sessionId" -ForegroundColor Yellow

# 1. 登录获取token
Write-Host "`n1. 登录获取token..." -ForegroundColor Cyan
$loginData = '{"username":"admin","password":"123456"}'

try {
    $loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/auth/login" -Method Post -Body $loginData -ContentType "application/json"
    $token = $loginResponse.token
    Write-Host "登录成功，获取到token" -ForegroundColor Green
} catch {
    Write-Host "登录失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 2. 开始会话
Write-Host "`n2. 开始操作会话..." -ForegroundColor Cyan
$startSessionData = '{"sessionId":"' + $sessionId + '","taskDetailId":1,"patientId":"TEST001","patientName":"test","phoneNumber":"13800138000"}'

try {
    $headers = @{
        "Authorization" = "Bearer $token"
        "Content-Type" = "application/json"
    }
    $startResponse = Invoke-RestMethod -Uri "$baseUrl/api/userbehavior/session/start" -Method Post -Body $startSessionData -Headers $headers
    Write-Host "会话开始成功" -ForegroundColor Green
} catch {
    Write-Host "会话开始失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 等待一下确保会话创建完成
Start-Sleep -Seconds 1

# 3. 模拟查看电话
Write-Host "`n3. 模拟查看电话..." -ForegroundColor Cyan
$timestamp1 = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
$viewPhoneData = '{"sessionId":"' + $sessionId + '","actionType":"VIEW_PHONE","targetElement":"contact_button","actionData":{"patientId":"TEST001","phoneNumber":"13800138000","timestamp":"' + $timestamp1 + '"},"pageUrl":"/refund-task-management"}'

try {
    $viewResponse = Invoke-RestMethod -Uri "$baseUrl/api/userbehavior/track" -Method Post -Body $viewPhoneData -Headers $headers
    Write-Host "查看电话行为记录成功" -ForegroundColor Green
} catch {
    Write-Host "查看电话行为记录失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 等待一下
Start-Sleep -Seconds 1

# 4. 模拟输入备注
Write-Host "`n4. 模拟输入备注..." -ForegroundColor Cyan
$timestamp2 = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
$inputNotesData = '{"sessionId":"' + $sessionId + '","actionType":"INPUT_NOTES","targetElement":"user_notes_1","actionData":{"text":"test notes","timestamp":"' + $timestamp2 + '"},"pageUrl":"/refund-task-management"}'

try {
    $inputResponse = Invoke-RestMethod -Uri "$baseUrl/api/userbehavior/track" -Method Post -Body $inputNotesData -Headers $headers
    Write-Host "输入备注行为记录成功" -ForegroundColor Green
} catch {
    Write-Host "输入备注行为记录失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 等待一下
Start-Sleep -Seconds 1

# 5. 模拟选择结果
Write-Host "`n5. 模拟选择结果..." -ForegroundColor Cyan
$timestamp3 = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
$selectResultData = '{"sessionId":"' + $sessionId + '","actionType":"SELECT_RESULT","targetElement":"contact_result_1","actionData":{"result":"contacted","contactIndex":1,"timestamp":"' + $timestamp3 + '"},"pageUrl":"/refund-task-management"}'

try {
    $selectResponse = Invoke-RestMethod -Uri "$baseUrl/api/userbehavior/track" -Method Post -Body $selectResultData -Headers $headers
    Write-Host "选择结果行为记录成功" -ForegroundColor Green
} catch {
    Write-Host "选择结果行为记录失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 等待一下
Start-Sleep -Seconds 1

# 6. 模拟保存联系
Write-Host "`n6. 模拟保存联系..." -ForegroundColor Cyan
$timestamp4 = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
$saveContactData = '{"sessionId":"' + $sessionId + '","actionType":"SAVE_CONTACT","targetElement":"save_button","actionData":{"userNotes":"test notes","contactResult":"contacted","patientId":"TEST001","timestamp":"' + $timestamp4 + '"},"pageUrl":"/refund-task-management"}'

try {
    $saveResponse = Invoke-RestMethod -Uri "$baseUrl/api/userbehavior/track" -Method Post -Body $saveContactData -Headers $headers
    Write-Host "保存联系行为记录成功" -ForegroundColor Green
} catch {
    Write-Host "保存联系行为记录失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 7. 检查数据库中的会话记录
Write-Host "`n7. 检查数据库中的会话记录..." -ForegroundColor Cyan
Start-Sleep -Seconds 2  # 等待数据库更新

$query = "SELECT session_id, phone_view_time, first_input_time, last_input_time, save_time, is_completed, risk_score FROM lzfy_user_operation_session WHERE session_id = '$sessionId'"

try {
    $result = sqlcmd -S "10.1.200.169,1434" -U sa -P "6hospital+" -d refunddb -Q $query
    Write-Host "数据库查询结果:" -ForegroundColor Yellow
    Write-Host $result -ForegroundColor White
} catch {
    Write-Host "数据库查询失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n测试完成！请查看服务日志以获取详细的调试信息。" -ForegroundColor Green